package View.MedianFilterDialog;

import View.MainFrame;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MedianFilterDialog extends JDialog implements ActionListener {
    MainFrame parentFrame;
    JRadioButton jrbtn1 = new JRadioButton("radius = 1");
    JRadioButton jrbtn2 = new JRadioButton("radius = 2");
    JRadioButton jrbtn3 = new JRadioButton("radius = 3");
    ButtonGroup btnGrRad = new ButtonGroup();
    JPanel jpRad = new JPanel(new BorderLayout());
    JButton jbtnOK = new JButton("OK");
    JButton jbtnCancel = new JButton("Cancel");

    boolean closeFlag;

    static final String OK_COMMAND = "OK_COMMAND";
    static final String CANCEL_COMMAND = "CANCEL_COMMAND";

    int r;

    public MedianFilterDialog(MainFrame parentFrame) {
        this.parentFrame = parentFrame;
        setLocationRelativeTo(parentFrame);
        setSize(250, 170);
        setResizable(false);
        setTitle("Median filter parameters");
        setModal(true);
        setVisible(false);



        jbtnOK.addActionListener(this);
        jbtnOK.setActionCommand(OK_COMMAND);

        jbtnCancel.addActionListener(this);
        jbtnCancel.setActionCommand(CANCEL_COMMAND);

        jrbtn1.setActionCommand("ONE");
        jrbtn1.addActionListener(this);
        jrbtn1.setSelected(true);
        jrbtn2.setActionCommand("TWO");
        jrbtn2.addActionListener(this);
        jrbtn3.setActionCommand("THREE");
        jrbtn3.addActionListener(this);
        jpRad.add(jrbtn1, BorderLayout.NORTH);
        jpRad.add(jrbtn2, BorderLayout.CENTER);
        jpRad.add(jrbtn3, BorderLayout.SOUTH);
        jpRad.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder("Select radius"), BorderFactory.createEmptyBorder(0, 5, 0, 5)));
        btnGrRad.add(jrbtn1);
        btnGrRad.add(jrbtn2);
        btnGrRad.add(jrbtn3);

        GridBagLayout gb = new GridBagLayout();
        setLayout(gb);

        gb.setConstraints(jpRad, new GridBagConstraints(0, 0, 3, 3, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 10, 10, 0), 0, 0));
        add(jpRad);

        gb.setConstraints(jbtnOK, new GridBagConstraints(0, 4, 2, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 10, 0, 10), 0, 0));
        add(jbtnOK);
        gb.setConstraints(jbtnCancel, new GridBagConstraints(2, 4, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 10, 0, 10), 0, 0));
        add(jbtnCancel);

    }

    public boolean runDialog() {
        closeFlag = false;
        setVisible(true);
        return  closeFlag;
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals(OK_COMMAND)) {
            closeFlag = true;
            setVisible(false);

        }
        if (e.getActionCommand().equals(CANCEL_COMMAND)) {
            setVisible(false);
        }
        if (e.getActionCommand().equals("ONE")) {
            r = 1;
        }
        if (e.getActionCommand().equals("TWO")) {
            r = 2;
        }
        if (e.getActionCommand().equals("THREE")) {
            r = 3;
        }


    }

    public int getR() {
        return r;
    }
}